package com.wgb.movie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgb.movie.common.ErrorCode;
import com.wgb.movie.constant.CommonConstant;
import com.wgb.movie.exception.ThrowUtils;
import com.wgb.movie.mapper.TheaterMapper;
import com.wgb.movie.model.dto.theater.TheaterQueryRequest;
import com.wgb.movie.model.entity.MovieShow;
import com.wgb.movie.model.entity.Theater;

import com.wgb.movie.model.vo.TheaterVO;
import com.wgb.movie.service.MovieShowService;
import com.wgb.movie.service.TheaterService;
import com.wgb.movie.service.UserService;
import com.wgb.movie.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 影院服务实现
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class TheaterServiceImpl extends ServiceImpl<TheaterMapper, Theater> implements TheaterService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param theater
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validTheater(Theater theater, boolean add) {
        ThrowUtils.throwIf(theater == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String theaterName = theater.getTheaterName();
        String theaterAddress = theater.getTheaterAddress();



        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(theaterName), ErrorCode.PARAMS_ERROR,"影院名称不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(theaterAddress), ErrorCode.PARAMS_ERROR,"影院地址不能为空");
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则

    }

    /**
     * 获取查询条件
     *
     * @param theaterQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Theater> getQueryWrapper(TheaterQueryRequest theaterQueryRequest) {
        QueryWrapper<Theater> queryWrapper = new QueryWrapper<>();
        if (theaterQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = theaterQueryRequest.getId();
        String theaterName = theaterQueryRequest.getTheaterName();
        String theaterAddress = theaterQueryRequest.getTheaterAddress();
        String sortField = theaterQueryRequest.getSortField();
        String sortOrder = theaterQueryRequest.getSortOrder();

        // todo 补充需要的查询条件
        // 从多字段中搜索

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(theaterName), "theaterName", theaterName);
        queryWrapper.like(StringUtils.isNotBlank(theaterAddress), "theaterAddress", theaterAddress);


        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取影院封装
     *
     * @param theater
     * @param request
     * @return
     */
    @Override
    public TheaterVO getTheaterVO(Theater theater, HttpServletRequest request) {
        // 对象转封装类
        TheaterVO theaterVO = TheaterVO.objToVo(theater);

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选

        // endregion

        return theaterVO;
    }

    /**
     * 分页获取影院封装
     *
     * @param theaterPage
     * @param request
     * @return
     */
    @Override
    public Page<TheaterVO> getTheaterVOPage(Page<Theater> theaterPage, HttpServletRequest request) {
        List<Theater> theaterList = theaterPage.getRecords();
        Page<TheaterVO> theaterVOPage = new Page<>(theaterPage.getCurrent(), theaterPage.getSize(), theaterPage.getTotal());
        if (CollUtil.isEmpty(theaterList)) {
            return theaterVOPage;
        }
        // 对象列表 => 封装对象列表
        List<TheaterVO> theaterVOList = theaterList.stream().map(theater -> {
            return TheaterVO.objToVo(theater);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选

        // endregion

        theaterVOPage.setRecords(theaterVOList);
        return theaterVOPage;
    }

//    @Resource
//    private MovieShowService movieShowService;
//
//
//    @Override
//    @Transactional
//    public boolean removeByIdT(long id) {
//        movieShowService.remove(new QueryWrapper<MovieShow>().eq("movie_id", id));
//        return removeById(id);
//    }

}
